<%--
  Created by IntelliJ IDEA.
  User: wei
  Date: 2019/5/20
  Time: 14:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <head>
        <title>下级列表</title>
        <script src="../../js/jquery-3.3.1.min.js" charset="utf-8"></script>
        <script src="../../js/cookie/jquery.cookie.js" charset="utf-8"></script>
        <script src="../../layui-v2.2.5/layui/layui.js" charset="utf-8"></script>
        <link rel="stylesheet" href="../../layui-v2.2.5/layui/css/layui.css" media="all">

    </head>
</head>
<body>
<br>
<form class="layui-form" action="">
    <input type="hidden" name="id" value="${id}">
    <div class="layui-form-item">
        <div class="layui-input-inline" style="width:80px">
            <button class="layui-btn"  lay-submit="" lay-filter="addSubordinate">添加下级</button>
        </div>
        <div class="layui-input-inline">
            <input type="text" name="userId" required  lay-verify="required" placeholder="请输入要添加的会员id" autocomplete="off" class="layui-input">
        </div>
    </div>
</form>
<hr>
<br><br>
<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right: 100px"  href="javascript:location.replace(location.href);" title="刷新">
<i class="layui-icon" style="line-height:38px">ဂ</i></a>

<form action="/shop/subordinate">
    <input type="hidden" name="id" value="${id}">

    <div class="layui-input-inline">
        <input type="text" name="name" id="name" value="${name}" placeholder="请输入名字" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-input-inline" style="width:80px">
        <button class="layui-btn"  lay-submit="" lay-filter="/shop/agent"><i class="layui-icon">&#xe615;</i>搜索</button>
    </div>
</form>

<div class="layui-form">
    <form class="layui-form" action="">
        <table class="layui-table">
            <thead>
            <tr>
                <th>会员id</th>
                <th>会员名</th>
                <th>操作</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
                    <c:forEach items="${pageUtil.list}" var="cl">
                <tr>
                    <td>${cl.id}</td>
                    <td>${cl.realName}</td>
                    <td>
                        <button class="layui-btn" type="button" onclick="del('${cl.id}');">解除关系</button>
                    <td>
                </tr>
                    </c:forEach>
            </tbody>
        </table>
        <input type="hidden" id="id" value="${id}">
    </form>
    <div id="fenye"></div>

    <div id="under" style="margin-top: 20rem"></div>
</div>
<script>
    layui.use('laypage', function(){
        var laypage = layui.laypage;
        laypage.render({
            elem: 'fenye'
            ,count: ${pageUtil.totalRows}
            ,limit:10//数据总数，从服务端得到
            ,curr:  ${pageUtil.currentPage}
            ,hash:true
            ,jump: function(obj, first){
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                var id=$("#id").val();
                //首次不执行
                if(!first){
                    window.location.href="/shop/subordinate?id="+id+"&page="+obj.curr+"&limit="+obj.limit;
                }
            }
        })
    })
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(addSubordinate)', function(data){

            console.log(data.field);
            $.ajax({
                url:'/shop/addSubordinate',
                type:'get', //GET
                async:true,    //或false,是否异步
                data:data.field,
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    /*console.log(xhr)
                     console.log('发送前')*/
                },
                success:function(data){
                    console.log(data);
                    if (data.code=='000000'){
                        layer.msg(data.msg,{time:1000,icon: 6},function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            location.replace(location.href);
                            //关闭当前frame
                            /*parent.layer.close(index);*/
                        });
                        // 获得frame索引

                    }else {
                        layer.msg(data.msg,{time:1000,icon: 5});
                    }
                    //console.log(data)
                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    //console.log('结束')
                }
            });
            return false;
        });
    })

    function del(id) {
        $.get("/shop/delSubordinate?userId="+id+"&id=${id}",function (data) {
            console.log(data);
            if (data.code=='000000'){
                layer.msg(data.msg,{time:1000,icon: 6},function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    location.replace(location.href);
                    //关闭当前frame
                    /*parent.layer.close(index);*/
                });
                // 获得frame索引

            }else {
                layer.msg(data.msg,{time:1000,icon: 5});
            }
            //console.log(data)
        });
    }

</script>
</body>
</html>

